//
//  PictureController.swift
//  BaoKanSwift
//
//  Created by uncle-R on 16/6/8.
//  Copyright © 2016年 uncle-R. All rights reserved.
//

import UIKit
import MJRefresh

class PictureController: BBBaseViewController,BBLabelDelegate,UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate{
    
    var topScrollView = UIScrollView()
    var tableView :UITableView?
    let P_ID = "BBPictureCell"
    var dataArr = [String]()
    
    let  titles = [
        [
            "classid" : "322",
            "classname" : "图话网文"
        ],
        [
            "classid" : "434",
            "classname": "精品封面"
        ],
        [
            "classid" : "350",
            "classname": "美女模特"
        ],
        [
            "classid" : "366",
            "classname": "游戏图库"
        ],
        [
            "classid" : "338",
            "classname": "娱乐八卦"
        ],
        [
            "classid" : "354",
            "classname": "社会百态"
        ],
        [
            "classid" : "357",
            "classname": "旅游视野"
        ],
        [
            "classid" : "433",
            "classname": "军事图秀"
        ]

    ]

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.hidden = true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        creatView()
        loadInfo()
    }
    
    func creatView(){
        
        topScrollView.frame = CGRectMake(0,0,UI_SCREEN_WIDTH,60)
        topScrollView.backgroundColor = UIColor.init(red: 212/255.0, green: 61/255.0, blue: 62/255.0, alpha: 1.0)
        topScrollView.bounces = false
        topScrollView.scrollEnabled = true
        topScrollView.showsVerticalScrollIndicator = false
        topScrollView.showsHorizontalScrollIndicator = false
        topScrollView.delegate = self
        topScrollView.contentSize = CGSizeMake(CGFloat(titles.count) * width + CGFloat(titles.count - 1) * offset, 0)
        view.addSubview(topScrollView)
        
        let tableView = UITableView(frame: CGRectMake(0, topScrollView.bottom, UI_SCREEN_WIDTH,self.view.height - 44 - 60), style: UITableViewStyle.Plain)
        self.tableView = tableView
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = UIView()
        tableView.estimatedRowHeight = 250 //预估行高
   
        tableView.registerClass(BBPictureCell.self, forCellReuseIdentifier: P_ID)
        tableView.mj_header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        view.addSubview(tableView)
    }
    
    func loadNewData(){
        tableView?.mj_footer.endRefreshing()
        tableView?.mj_header.endRefreshing()
    }
    
    func loadMoreData(){
        tableView?.mj_footer.endRefreshing()
        tableView?.mj_header.endRefreshing()
    }
    
    func loadInfo(){
        
        for i in 0..<titles.count {
            
            let label = BBLabel()
            label.text = titles[i]["classname"]
            label.frame = CGRectMake(offset + self.width * CGFloat(i), topScrollView.size.height/2 - height/2+5, width, height)
            label.delegate = self
            label.tag = i + 2000 ///(1-1000tag 值  apple有可能自用)
            if i == 0 {
                label.didSelected = true
            }
            topScrollView.addSubview(label)
        }
    }
}

extension PictureController
    
{
    var width :CGFloat {
        get {
            return 70
        }
        set(newValue){
            self.width = newValue
        }
        
    }
    var height :CGFloat {
        get {
            return 40
        }
        set(newValue){
            self.height = newValue
        }
    }
    var top :CGFloat {
        set (newValue){
            self.top = newValue
        }
        get {
            return 10
            
        }
    }
    var offset :CGFloat {
        set (newValue){
            self.offset = newValue
        }
        get {
            return 10
            
        }
    }
    
     // MARK: - -------------------- DataSource,Delegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return 1
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(P_ID) as! BBPictureCell
        cell.pictureIMG.sd_setImageWithURL(NSURL(string:"http://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gbk&pn=9&rn=1&di=90270343380&ln=30&word=ͼƬ&os=908341833,992426952&cs=1642940013,2147239593&objurl=http%3A%2F%2Fwww.wed114.cn%2Fjiehun%2Fuploads%2Fallimg%2F160426%2F39_160426110624_1.jpg&bdtype=11&simid=0,0&fr=ala&ala=1&alatpl=others&pos=1)"), placeholderImage: UIImage(named: "zrx3.jpg"))
        cell.title.text = "一样的很没劲"
        return cell
    }
    
    //-----------------------------------------------TableView's Delegate-------------------------------------------
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 250;
    }
    

    
    // MARK: - BBLabel's Delegate

func labelClick(label: BBLabel, tag: Int) {

    var offsetX = label.center.x -  CGFloat(UI_SCREEN_WIDTH) / 2
    let offsetMax = topScrollView.contentSize.width - UI_SCREEN_WIDTH
    
    if offsetX < 0 {
        offsetX = 0
    }
        
    else if offsetX > offsetMax {
        offsetX = offsetMax
    }
    
    topScrollView.setContentOffset(CGPointMake(offsetX, topScrollView.contentOffset.y), animated: true)
    /* 恢复其他Label
     */
    for i in 0..<titles.count {
        //
        if i != tag - 2000  {
            if let label = topScrollView.viewWithTag(i+2000) as? BBLabel{
                print("asdasd\(label)")
                label.didSelected = false
            }
        }else{
            label.didSelected = true
        }
    }
}
}
